home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gshtscr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  20.6 KB  |  596 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gshtscr.c,v 1.3.2.2 2000/11/29 14:38:00 igorm Exp $ */
  20. /* Screen (Type 1) halftone processing for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxarith.h"
  26. #include "gzstate.h"
  27. #include "gxdevice.h"           /* for gzht.h */
  28. #include "gzht.h"
  29.  
  30. /* Define whether to force all halftones to be strip halftones, */
  31. /* for debugging. */
  32. static bool FORCE_STRIP_HALFTONES = false;
  33.  
  34. /* Structure descriptors */
  35. private_st_gs_screen_enum();
  36.  
  37. /* GC procedures */
  38. private 
  39. ENUM_PTRS_WITH(screen_enum_enum_ptrs, gs_screen_enum *eptr)
  40. {
  41.     if (index < 1 + st_ht_order_max_ptrs) {
  42.         gs_ptr_type_t ret =
  43.             ENUM_USING(st_ht_order, &eptr->order, sizeof(eptr->order),
  44.                        index - 1);
  45.  
  46.         if (ret == 0)           /* don't stop early */
  47.             ENUM_RETURN(0);
  48.         return ret;
  49.     }
  50.     return ENUM_USING(st_halftone, &eptr->halftone, sizeof(eptr->halftone),
  51.                       index - (1 + st_ht_order_max_ptrs));
  52. }
  53. ENUM_PTR(0, gs_screen_enum, pgs);
  54. ENUM_PTRS_END
  55. private RELOC_PTRS_WITH(screen_enum_reloc_ptrs, gs_screen_enum *eptr)
  56. {
  57.     RELOC_PTR(gs_screen_enum, pgs);
  58.     RELOC_USING(st_halftone, &eptr->halftone, sizeof(gs_halftone));
  59.     RELOC_USING(st_ht_order, &eptr->order, sizeof(gx_ht_order));
  60. }
  61. RELOC_PTRS_END
  62.  
  63. /* Define the default value of AccurateScreens that affects */
  64. /* setscreen and setcolorscreen. */
  65. private bool screen_accurate_screens;
  66.  
  67. /* Default AccurateScreens control */
  68. void
  69. gs_setaccuratescreens(bool accurate)
  70. {
  71.     screen_accurate_screens = accurate;
  72. }
  73. bool
  74. gs_currentaccuratescreens(void)
  75. {
  76.     return screen_accurate_screens;
  77. }
  78.  
  79. /* Define the MinScreenLevels user parameter similarly. */
  80. private uint screen_min_screen_levels;
  81.  
  82. void
  83. gs_setminscreenlevels(uint levels)
  84. {
  85.     screen_min_screen_levels = levels;
  86. }
  87. uint
  88. gs_currentminscreenlevels(void)
  89. {
  90.     return screen_min_screen_levels;
  91. }
  92.  
  93. /* Initialize the screen control statics at startup. */
  94. init_proc(gs_gshtscr_init);     /* check prototype */
  95. int
  96. gs_gshtscr_init(gs_memory_t *mem)
  97. {
  98.     gs_setaccuratescreens(false);
  99.     gs_setminscreenlevels(1);
  100.     return 0;
  101. }
  102.  
  103. /*
  104.  * The following implementation notes complement the general discussion of
  105.  * halftone tiles found in gxdht.h.
  106.  *
  107.  * Currently we allow R(') > 1 (i.e., multiple basic cells per multi-cell)
  108.  * only if AccurateScreens is true or if B (the number of pixels in a basic
  109.  * cell) < MinScreenLevels; if AccurateScreens is false and B >=
  110.  * MinScreenLevels, multi-cells and basic cells are the same.
  111.  *
  112.  * To find the smallest super-cell for a given multi-cell size, i.e., the
  113.  * smallest (absolute value) coordinates where the corners of multi-cells
  114.  * lie on the coordinate axes, we compute the values of i and j that give
  115.  * the minimum value of W by:
  116.  *      D = gcd(abs(M'), abs(N)), i = M'/D, j = N/D, W = C / D,
  117.  * and similarly
  118.  *      D' = gcd(abs(M), abs(N')), i' = N'/D', j' = M/D', W' = C / D'.
  119.  */
  120.  
  121. /* Compute the derived values of a halftone tile. */
  122. void
  123. gx_compute_cell_values(gx_ht_cell_params_t * phcp)
  124. {
  125.     const int M = phcp->M, N = phcp->N, M1 = phcp->M1, N1 = phcp->N1;
  126.     const uint m = any_abs(M), n = any_abs(N);
  127.     const uint m1 = any_abs(M1), n1 = any_abs(N1);
  128.     const ulong C = phcp->C = (ulong)m * m1 + (ulong)n * n1;
  129.     const int D = phcp->D = igcd(m1, n);
  130.     const int D1 = phcp->D1 = igcd(m, n1);
  131.  
  132.     phcp->W = C / D, phcp->W1 = C / D1;
  133.     /* Compute the shift value. */
  134.     /* If M1 or N is zero, the shift is zero. */
  135.     if (M1 && N) {
  136.         int h = 0, k = 0, dy = 0;
  137.         int shift;
  138.  
  139.         /*
  140.          * There may be a faster way to do this: see Knuth vol. 2,
  141.          * section 4.5.2, Algorithm X (p. 302) and exercise 15
  142.          * (p. 315, solution p. 523).
  143.          */
  144.         while (dy != D)
  145.             if (dy > D) {
  146.                 if (M1 > 0)
  147.                     ++k;
  148.                 else
  149.                     --k;
  150.                 dy -= m1;
  151.             } else {
  152.                 if (N > 0)
  153.                     ++h;
  154.                 else
  155.                     --h;
  156.                 dy += n;
  157.             }
  158.         shift = h * M + k * N1;
  159.         /* We just computed what amounts to a right shift; */
  160.         /* what we want is a left shift. */
  161.         phcp->S = imod(-shift, phcp->W);
  162.     } else
  163.         phcp->S = 0;
  164.     if_debug12('h', "[h]MNR=(%d,%d)/%d, M'N'R'=(%d,%d)/%d => C=%lu, D=%d, D'=%d, W=%u, W'=%u, S=%d\n",
  165.                M, N, phcp->R, M1, N1, phcp->R1,
  166.                C, D, D1, phcp->W, phcp->W1, phcp->S);
  167. }
  168.  
  169. /* Forward references */
  170. private int pick_cell_size(P6(gs_screen_halftone * ph,
  171.      const gs_matrix * pmat, ulong max_size, uint min_levels, bool accurate,
  172.                               gx_ht_cell_params_t * phcp));
  173.  
  174. /* Allocate a screen enumerator. */
  175. gs_screen_enum *
  176. gs_screen_enum_alloc(gs_memory_t * mem, client_name_t cname)
  177. {
  178.     return gs_alloc_struct(mem, gs_screen_enum, &st_gs_screen_enum, cname);
  179. }
  180.  
  181. /* Set up for halftone sampling. */
  182. int
  183. gs_screen_init(gs_screen_enum * penum, gs_state * pgs,
  184.                gs_screen_halftone * phsp)
  185. {
  186.     return gs_screen_init_accurate(penum, pgs, phsp,
  187.                                    screen_accurate_screens);
  188. }
  189. int
  190. gs_screen_init_memory(gs_screen_enum * penum, gs_state * pgs,
  191.                 gs_screen_halftone * phsp, bool accurate, gs_memory_t * mem)
  192. {
  193.     int code =
  194.     gs_screen_order_init_memory(&penum->order, pgs, phsp, accurate, mem);
  195.  
  196.     if (code < 0)
  197.         return code;
  198.     return
  199.         gs_screen_enum_init_memory(penum, &penum->order, pgs, phsp, mem);
  200. }
  201.  
  202. /* Allocate and initialize a spot screen. */
  203. /* This is the first half of gs_screen_init_accurate. */
  204. int
  205. gs_screen_order_alloc(gx_ht_order *porder, gs_memory_t *mem)
  206. {
  207.     uint num_levels = porder->params.W * porder->params.D;
  208.     int code;
  209.  
  210.     if (!FORCE_STRIP_HALFTONES &&
  211.         ((ulong)porder->params.W1 * bitmap_raster(porder->params.W) +
  212.            num_levels * sizeof(*porder->levels) +
  213.            porder->params.W * porder->params.W1 * sizeof(gx_ht_bit)) <=
  214.         porder->screen_params.max_size) {
  215.         /*
  216.          * Allocate an order for the entire tile, but only sample one
  217.          * strip.  Note that this causes the order parameters to be
  218.          * self-inconsistent until gx_ht_construct_spot_order fixes them
  219.          * up: see gxdht.h for more information.
  220.          */
  221.         code = gx_ht_alloc_order(porder, porder->params.W,
  222.                                  porder->params.W1, 0,
  223.                                  num_levels, mem);
  224.         porder->height = porder->orig_height = porder->params.D;
  225.         porder->shift = porder->orig_shift = porder->params.S;
  226.     } else {
  227.         /* Just allocate the order for a single strip. */
  228.         code = gx_ht_alloc_order(porder, porder->params.W,
  229.                                  porder->params.D, porder->params.S,
  230.                                  num_levels, mem);
  231.     }
  232.     return code;
  233. }
  234. int
  235. gs_screen_order_init_memory(gx_ht_order * porder, const gs_state * pgs,
  236.                             gs_screen_halftone * phsp, bool accurate,
  237.                             gs_memory_t * mem)
  238. {
  239.     gs_matrix imat;
  240.     ulong max_size = pgs->ht_cache->bits_size;
  241.     int code;
  242.  
  243.     if (phsp->frequency < 0.1)
  244.         return_error(gs_error_rangecheck);
  245.     gs_deviceinitialmatrix(gs_currentdevice(pgs), &imat);
  246.     code = pick_cell_size(phsp, &imat, max_size,
  247.                           screen_min_screen_levels, accurate,
  248.                           &porder->params);
  249.     if (code < 0)
  250.         return code;
  251.     gx_compute_cell_values(&porder->params);
  252.     porder->screen_params.matrix = imat;
  253.     porder->screen_params.max_size = max_size;
  254.     return gs_screen_order_alloc(porder, mem);
  255. }
  256.  
  257. /*
  258.  * Given a desired frequency, angle, and minimum number of levels, a maximum
  259.  * cell size, and an AccurateScreens flag, pick values for M('), N('), and
  260.  * R(').  We want to get a good fit to the requested frequency and angle,
  261.  * provide at least the requested minimum number of levels, and keep
  262.  * rendering as fast as possible; trading these criteria off against each
  263.  * other is what makes the code complicated.
  264.  *
  265.  * We compute trial values u and v from the original values of F and A.
  266.  * Normally these will not be integers.  We then examine the 4 pairs of
  267.  * integers obtained by rounding each of u and v independently up or down,
  268.  * and pick the pair U, V that yields the closest match to the requested
  269.  * F and A values and doesn't require more than max_size storage for a
  270.  * single tile.  If no pair
  271.  * yields an acceptably small W, we divide both u and v by 2 and try again.
  272.  * Then we run the equations backward to obtain the actual F and A.
  273.  * This is fairly easy given that we require either xx = yy = 0 or
  274.  * xy = yx = 0.  In the former case, we have
  275.  *      U = (72 / F * xx) * cos(A);
  276.  *      V = (72 / F * yy) * sin(A);
  277.  * from which immediately
  278.  *      A = arctan((V / yy) / (U / xx)),
  279.  * or equivalently
  280.  *      A = arctan((V * xx) / (U * yy)).
  281.  * We can then obtain F as
  282.  *      F = (72 * xx / U) * cos(A),
  283.  * or equivalently
  284.  *      F = (72 * yy / V) * sin(A).
  285.  * For landscape devices, we replace xx by yx, yy by xy, and interchange
  286.  * sin and cos, resulting in
  287.  *      A = arctan((U * xy) / (V * yx))
  288.  * and
  289.  *      F = (72 * yx / U) * sin(A)
  290.  * or
  291.  *      F = (72 * xy / V) * cos(A).
  292.  */
  293. /* ph->frequency and ph->angle are input parameters; */
  294. /* the routine sets ph->actual_frequency and ph->actual_angle. */
  295. private int
  296. pick_cell_size(gs_screen_halftone * ph, const gs_matrix * pmat, ulong max_size,
  297.                uint min_levels, bool accurate, gx_ht_cell_params_t * phcp)
  298. {
  299.     const bool landscape = (pmat->xy != 0.0 || pmat->yx != 0.0);
  300.  
  301.     /* Account for a possibly reflected coordinate system. */
  302.     /* See gxstroke.c for the algorithm. */
  303.     const bool reflected = pmat->xy * pmat->yx > pmat->xx * pmat->yy;
  304.     const int reflection = (reflected ? -1 : 1);
  305.     const int rotation =
  306.     (landscape ? (pmat->yx < 0 ? 90 : -90) : pmat->xx < 0 ? 180 : 0);
  307.     const double f0 = ph->frequency, a0 = ph->angle;
  308.     const double T =
  309.     fabs((landscape ? pmat->yx / pmat->xy : pmat->xx / pmat->yy));
  310.     gs_point uv0;
  311.  
  312. #define u0 uv0.x
  313. #define v0 uv0.y
  314.     int rt = 1;
  315.     double f = 0, a = 0;
  316.     double e_best = 1000;
  317.     bool better;
  318.  
  319.     /*
  320.      * We need to find a vector in device space whose length is
  321.      * 1 inch / ph->frequency and whose angle is ph->angle.
  322.      * Because device pixels may not be square, we can't simply
  323.      * map the length to device space and then rotate it;
  324.      * instead, since we know that user space is uniform in X and Y,
  325.      * we calculate the correct angle in user space before rotation.
  326.      */
  327.  
  328.     /* Compute trial values of u and v. */
  329.  
  330.     {
  331.         gs_matrix rmat;
  332.  
  333.         gs_make_rotation(a0 * reflection + rotation, &rmat);
  334.         gs_distance_transform(72.0 / f0, 0.0, &rmat, &uv0);
  335.         gs_distance_transform(u0, v0, pmat, &uv0);
  336.         if_debug10('h', "[h]Requested: f=%g a=%g mat=[%g %g %g %g] max_size=%lu min_levels=%u =>\n     u=%g v=%g\n",
  337.                    ph->frequency, ph->angle,
  338.                    pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  339.                    max_size, min_levels, u0, v0);
  340.     }
  341.  
  342.     /* Adjust u and v to reasonable values. */
  343.  
  344.     if (u0 == 0 && v0 == 0)
  345.         return_error(gs_error_rangecheck);
  346.     while ((fabs(u0) + fabs(v0)) * rt < 4)
  347.         ++rt;
  348.   try_size:
  349.     better = false;
  350.     {
  351.         int m0 = (int)floor(u0 * rt + 0.0001);
  352.         int n0 = (int)floor(v0 * rt + 0.0001);
  353.         gx_ht_cell_params_t p;
  354.  
  355.         p.R = p.R1 = rt;
  356.         for (p.M = m0 + 1; p.M >= m0; p.M--)
  357.             for (p.N = n0 + 1; p.N >= n0; p.N--) {
  358.                 long raster, wt, wt_size;
  359.                 double fr, ar, ft, at, f_diff, a_diff, f_err, a_err;
  360.  
  361.                 p.M1 = (int)floor(p.M / T + 0.5);
  362.                 p.N1 = (int)floor(p.N * T + 0.5);
  363.                 gx_compute_cell_values(&p);
  364.                 if_debug3('h', "[h]trying m=%d, n=%d, r=%d\n", p.M, p.N, rt);
  365.                 wt = p.W;
  366.                 if (wt >= max_short)
  367.                     continue;
  368.                 /* Check the strip size, not the full tile size, */
  369.                 /* against max_size. */
  370.                 raster = bitmap_raster(wt);
  371.                 if (raster > max_size / p.D || raster > max_long / wt)
  372.                     continue;
  373.                 wt_size = raster * wt;
  374.  
  375.                 /* Compute the corresponding values of F and A. */
  376.  
  377.                 if (landscape)
  378.                     ar = atan2(p.M * pmat->xy, p.N * pmat->yx),
  379.                         fr = 72.0 * (p.M == 0 ? pmat->xy / p.N * cos(ar) :
  380.                                      pmat->yx / p.M * sin(ar));
  381.                 else
  382.                     ar = atan2(p.N * pmat->xx, p.M * pmat->yy),
  383.                         fr = 72.0 * (p.M == 0 ? pmat->yy / p.N * sin(ar) :
  384.                                      pmat->xx / p.M * cos(ar));
  385.                 ft = fabs(fr) * rt;
  386.                 /* Normalize the angle to the requested quadrant. */
  387.                 at = (ar * radians_to_degrees - rotation) * reflection;
  388.                 at -= floor(at / 180.0) * 180.0;
  389.                 at += floor(a0 / 180.0) * 180.0;
  390.                 f_diff = fabs(ft - f0);
  391.                 a_diff = fabs(at - a0);
  392.                 f_err = f_diff / fabs(f0);
  393.                 /*
  394.                  * We used to compute the percentage difference here:
  395.                  *      a_err = (a0 == 0 ? a_diff : a_diff / fabs(a0));
  396.                  * but using the angle difference makes more sense:
  397.                  */
  398.                 a_err = a_diff;
  399.  
  400.                 if_debug5('h', " ==> d=%d, wt=%ld, wt_size=%ld, f=%g, a=%g\n",
  401.                           p.D, wt, bitmap_raster(wt) * wt, ft, at);
  402.  
  403.                 /*
  404.                  * Minimize angle and frequency error within the
  405.                  * permitted maximum super-cell size.
  406.                  */
  407.  
  408.                 {
  409.                     double err = f_err * a_err;
  410.  
  411.                     if (err > e_best)
  412.                         continue;
  413.                     e_best = err;
  414.                 }
  415.                 *phcp = p;
  416.                 f = ft, a = at;
  417.                 better = true;
  418.                 if_debug3('h', "*** best wt_size=%ld, f_diff=%g, a_diff=%g\n",
  419.                           wt_size, f_diff, a_diff);
  420.                 /*
  421.                  * We want a maximum relative frequency error of 1% and a
  422.                  * maximum angle error of 1% (of 90 degrees).
  423.                  */
  424.                 if (f_err <= 0.01 && a_err <= 0.9 /*degrees*/)
  425.                     goto done;
  426.             }
  427.     }
  428.     if (phcp->C < min_levels) { /* We don't have enough levels yet.  Keep going. */
  429.         ++rt;
  430.         goto try_size;
  431.     }
  432.     if (better) {               /* If we want accurate screens, continue till we fail. */
  433.         if (accurate) {
  434.             ++rt;
  435.             goto try_size;
  436.         }
  437.     } else {                    /*
  438.                                  * We couldn't find an acceptable M and N.  If R > 1,
  439.                                  * take what we've got; if R = 1, give up.
  440.                                  */
  441.         if (rt == 1)
  442.             return_error(gs_error_rangecheck);
  443.     }
  444.  
  445.     /* Deliver the results. */
  446.   done:
  447.     if_debug5('h', "[h]Chosen: f=%g a=%g M=%d N=%d R=%d\n",
  448.               f, a, phcp->M, phcp->N, phcp->R);
  449.     ph->actual_frequency = f;
  450.     ph->actual_angle = a;
  451.     return 0;
  452. #undef u0
  453. #undef v0
  454. }
  455.  
  456. /* Prepare to sample a spot screen. */
  457. /* This is the second half of gs_screen_init_accurate. */
  458. int
  459. gs_screen_enum_init_memory(gs_screen_enum * penum, const gx_ht_order * porder,
  460.                            gs_state * pgs, const gs_screen_halftone * phsp,
  461.                            gs_memory_t * mem)
  462. {
  463.     penum->pgs = pgs;           /* ensure clean for GC */
  464.     penum->order = *porder;
  465.     penum->halftone.rc.memory = mem;
  466.     penum->halftone.type = ht_type_screen;
  467.     penum->halftone.params.screen = *phsp;
  468.     penum->x = penum->y = 0;
  469.     penum->strip = porder->num_levels / porder->width;
  470.     penum->shift = porder->shift;
  471.     /*
  472.      * We want a transformation matrix that maps the parallelogram
  473.      * (0,0), (U,V), (U-V',V+U'), (-V',U') to the square (+/-1, +/-1).
  474.      * If the coefficients are [a b c d e f] and we let
  475.      *      u = U = M/R, v = V = N/R,
  476.      *      r = -V' = -N'/R', s = U' = M'/R',
  477.      * then we just need to solve the equations:
  478.      *      a*0 + c*0 + e = -1      b*0 + d*0 + f = -1
  479.      *      a*u + c*v + e = 1       b*u + d*v + f = 1
  480.      *      a*r + c*s + e = -1      b*r + d*s + f = 1
  481.      * This has the following solution:
  482.      *      Q = 2 / (M*M' + N*N')
  483.      *      a = Q * R * M'
  484.      *      b = -Q * R' * N
  485.      *      c = Q * R * N'
  486.      *      d = Q * R' * M
  487.      *      e = -1
  488.      *      f = -1
  489.      */
  490.     {
  491.         const int M = porder->params.M, N = porder->params.N, R = porder->params.R;
  492.         const int M1 = porder->params.M1, N1 = porder->params.N1, R1 = porder->params.R1;
  493.         double Q = 2.0 / ((long)M * M1 + (long)N * N1);
  494.  
  495.         penum->mat.xx = Q * (R * M1);
  496.         penum->mat.xy = Q * (-R1 * N);
  497.         penum->mat.yx = Q * (R * N1);
  498.         penum->mat.yy = Q * (R1 * M);
  499.         penum->mat.tx = -1.0;
  500.         penum->mat.ty = -1.0;
  501.         gs_matrix_invert(&penum->mat, &penum->mat_inv);
  502.     }
  503.     if_debug7('h', "[h]Screen: (%dx%d)/%d [%f %f %f %f]\n",
  504.               porder->width, porder->height, porder->params.R,
  505.               penum->mat.xx, penum->mat.xy,
  506.               penum->mat.yx, penum->mat.yy);
  507.     return 0;
  508. }
  509.  
  510. /* Report current point for sampling */
  511. int
  512. gs_screen_currentpoint(gs_screen_enum * penum, gs_point * ppt)
  513. {
  514.     gs_point pt;
  515.     int code;
  516.     double sx, sy; /* spot center in spot coords (integers) */
  517.     gs_point spot_center; /* device coords */
  518.  
  519.     if (penum->y >= penum->strip) {     /* all done */
  520.         gx_ht_construct_spot_order(&penum->order);
  521.         return 1;
  522.     }
  523.     /* We displace the sampled coordinates very slightly */
  524.     /* in order to reduce the likely number of points */
  525.     /* for which the spot function returns the same value. */
  526.     if ((code = gs_point_transform(penum->x + 0.501, penum->y + 0.498, &penum->mat, &pt)) < 0)
  527.         return code;
  528.  
  529.     /* find the spot center in device coords : */
  530.     sx = ceil( pt.x / 2 ) * 2;
  531.     sy = ceil( pt.y / 2 ) * 2;
  532.     if ((code = gs_point_transform(sx, sy, &penum->mat_inv, &spot_center)) < 0)
  533.         return code;
  534.  
  535.     /* shift the spot center to nearest pixel center : */
  536.     spot_center.x = floor(spot_center.x) + 0.5;
  537.     spot_center.y = floor(spot_center.y) + 0.5;
  538.  
  539.     /* compute the spot function arguments for the shifted spot : */
  540.     if ((code = gs_distance_transform(penum->x - spot_center.x + 0.501, 
  541.                                       penum->y - spot_center.y + 0.498,
  542.                                       &penum->mat, &pt)) < 0)
  543.         return code;
  544.     pt.x += 1;
  545.     pt.y += 1;
  546.  
  547.     if (pt.x < -1.0)
  548.         pt.x += ((int)(-ceil(pt.x)) + 1) & ~1;
  549.     else if (pt.x >= 1.0)
  550.         pt.x -= ((int)pt.x + 1) & ~1;
  551.     if (pt.y < -1.0)
  552.         pt.y += ((int)(-ceil(pt.y)) + 1) & ~1;
  553.     else if (pt.y >= 1.0)
  554.         pt.y -= ((int)pt.y + 1) & ~1;
  555.     *ppt = pt;
  556.     return 0;
  557. }
  558.  
  559. /* Record next halftone sample */
  560. int
  561. gs_screen_next(gs_screen_enum * penum, floatp value)
  562. {
  563.     ht_sample_t sample;
  564.     int width = penum->order.width;
  565.     gx_ht_bit *bits = (gx_ht_bit *)penum->order.bit_data;
  566.  
  567.     if (value < -1.0 || value > 1.0)
  568.         return_error(gs_error_rangecheck);
  569.     sample = (ht_sample_t) ((value + 1) * max_ht_sample);
  570. #ifdef DEBUG
  571.     if (gs_debug_c('H')) {
  572.         gs_point pt;
  573.  
  574.         gs_screen_currentpoint(penum, &pt);
  575.         dlprintf6("[H]sample x=%d y=%d (%f,%f): %f -> %u\n",
  576.                   penum->x, penum->y, pt.x, pt.y, value, sample);
  577.     }
  578. #endif
  579.     bits[penum->y * width + penum->x].mask = sample;
  580.     if (++(penum->x) >= width)
  581.         penum->x = 0, ++(penum->y);
  582.     return 0;
  583. }
  584.  
  585. /* Install a fully constructed screen in the gstate. */
  586. int
  587. gs_screen_install(gs_screen_enum * penum)
  588. {
  589.     gx_device_halftone dev_ht;
  590.  
  591.     dev_ht.rc.memory = penum->halftone.rc.memory;
  592.     dev_ht.order = penum->order;
  593.     dev_ht.components = 0;
  594.     return gx_ht_install(penum->pgs, &penum->halftone, &dev_ht);
  595. }
  596.